package algorithm;

/**
 * @author fangkun
 * @create 2022-05-20-10:46
 *
 * 35
 */
public class SearchInsertPosition {
    public static void main(String[] args) {
        int i = searchInsert(new int[]{2, 4, 5, 5, 9}, 4);
        System.out.println(i);
    }
    public static int searchInsert(int[] nums, int target) {

        int left = 0,right = nums.length - 1;

        while (left <= right){

            int mid = left + (right-left)/2;

            if (nums[mid] > target){
                right = mid - 1;
            }else if(nums[mid] < target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
        }
        return left;
    }

}
